package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.query.HotelSearchQueryObject;
import cn.wolfcode.luowowo.search.repository.DestinationTemplateSearchRepository;
import cn.wolfcode.luowowo.search.repository.HotelTemplateSearchRepository;
import cn.wolfcode.luowowo.search.service.HotelTemplateSearchService;
import cn.wolfcode.luowowo.search.template.HotelTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;

import java.util.Collections;
import java.util.List;


/**
 * 酒店查询
 *
 * @author win
 */
@Service
public class HotelTemplateSearchServiceImpl implements HotelTemplateSearchService {
    @Autowired
    private HotelTemplateSearchRepository repository;

    @Autowired
    private ElasticsearchTemplate template;


    @Override
    public void saveOrUpdate(HotelTemplate hotel) {
        repository.save(hotel);
    }

    @Override
    public List<HotelTemplate> queryByHotelThemeName(String themeName) {
        return repository.findByThemeName(themeName);
    }

    @Override
    public List<HotelTemplate> queryByHotelDestName(String destName) {
        List<HotelTemplate> byDestName = repository.findByDestName(destName);
        return byDestName;
    }

    @Override
    public List<HotelTemplate> queryByDestinationId(Long destId) {
        return repository.findByDestId(destId);
    }

    @Override
    public Page<HotelTemplate> searchByQo(HotelSearchQueryObject qo) {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        if (qo.getMinPrice() != 0.0) {
            query.must(QueryBuilders.rangeQuery("price").gte(qo.getMinPrice()).lte(qo.getMaxPrice()));
        }
        if (qo.getThemeName() != null) {
            query.must(QueryBuilders.matchQuery("themeName", qo.getThemeName()));
        }
        if (qo.getThemeId() != -1L) {
            query.must(QueryBuilders.matchQuery("themeId", qo.getThemeId()));
        }
        if (qo.getDestId() != -1L) {
            query.must(QueryBuilders.matchQuery("destId", qo.getDestId()));
        }
        if (qo.getDestName() != null) {
            query.must(QueryBuilders.matchQuery("destName", qo.getDestName()));
        }
        if (qo.getKeyword() != null) {
            query.must(QueryBuilders.multiMatchQuery(qo.getKeyword(), "hotelInfo", "hotelName", "address"));
        }
        builder.withQuery(query);
        PageRequest of;
        if (qo.getOrderFlag() == 3) {
            of = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.DESC, "score");
        } else {
            of = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), qo.getOrderFlag() == 1 ? Sort.Direction.DESC : Sort.Direction.ASC, "price");
        }
        builder.withPageable(of);
        Page<HotelTemplate> search = repository.search(builder.build());
        return search;
    }

    @Override
    public List<HotelTemplate> queryByDestIdTop8InScore(Long cityId) {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.matchQuery("destId", cityId));
        builder.withPageable(PageRequest.of(0, 8, Sort.Direction.DESC, "score"));
        List<HotelTemplate> content = repository.search(builder.build()).getContent();
        return content == null ? Collections.emptyList() : content;
    }

    @Override
    public List<HotelTemplate> getHotelByHotelIds(List<Long> hotelIds) {
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        builder.withQuery(QueryBuilders.termsQuery("id", hotelIds));
        List<HotelTemplate> content = repository.search(builder.build()).getContent();
        return content == null ? Collections.emptyList() : content;
    }
}
